<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Cart extends MY_Controller
{
    /**
     * Index Page for this controller.
     */
    public function index()
    {
        $this->load->library('c_product_list');

        $sendData = array();
        $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
        $sendData['channelId'] = $this->lg_user['channelId'];
        $sendData['userType'] = $this->lg_user['type'];
        $sendData['uid'] = $this->lg_user['id'];
        $sendData['password'] = $this->lg_user['password'];

        //制造接口请求
        $url = $this->c_product_list->set_call_items($sendData);
        $param = $this->c_product_list->get_call_items();
        $rs = $this->restcurl->get($url, $param);
        if ($rs && $rs['ErrorCode'] == 0 && $rs['Success'] == 1) {
            $this->c_product_list->set_back_items($rs['CallInfo']);
        }
        $CallInfo = $this->c_product_list->get_back_items();
        $productList = isset($CallInfo['product_list']) ? $CallInfo['product_list'] : array();

        //购物车
        $cartData = json_decode(gCookie('user_cart'), true);
        $productData = $productIds = $skuIds = array();
        if (count($cartData) > 0) {
            foreach ($cartData as $key => $val) {
                $keyArr = explode('_', $key);
                $product_id = $keyArr[0];
                $sku_id = $keyArr[1];

                $skuIds[$sku_id]['product_id'] = $product_id;
                $skuIds[$sku_id]['num'] = $val;
                $productIds[] = $product_id;
            }
            $productIds = array_unique($productIds);
            foreach ($productList as $k => $v) {
                if (in_array($v['id'], $productIds)) {
                    $productData[$v['id']] = $v;
                }
            }
        }

        $this->data['productData'] = $productData;
        $this->data['productIds'] = $productIds;
        $this->data['skuIds'] = $skuIds;
        $this->render('cart/index', $this->data);
    }
}
